const {
  override,
  addLessLoader,
  useBabelRc,
  useEslintRc,
  adjustStyleLoaders
} = require('customize-cra');
const path = require("path");
console.log('+++', path.resolve(__dirname, './.eslintrc.js'))
// eslint config
const eslintConfig = require('./.eslintrc.js');
const useEslintConfig = configRules => config => {
  const updatedRules = config.module.rules.map(
    rule => {
      // Only target rules that have defined a `useEslintrc` parameter in their options
      if (rule.use && rule.use.some(use => use.options && use.options.useEslintrc !== void 0)) {
        console.log('6666', rule)
        const ruleUse = rule.use[0]
        const baseOptions = ruleUse.options
        const baseConfig = baseOptions.baseConfig || {}
        const newOptions = {
          useEslintrc: false,
          ignore: true,
          baseConfig: { ...baseConfig, ...configRules },
        }
        ruleUse.options = newOptions
        return rule

        // Rule not using eslint. Do not modify.
      } else {
        return rule
      }
    }
  )

  config.module.rules = updatedRules;
  return config;
}

module.exports = override(
    addLessLoader({  
        javascriptEnabled: true,
        modifyVars: { 'primary-color': '#f9c700' },
        sourceMap:true,
        localIdentName: '[local]--[hash:base64:5]'
    }),
    adjustStyleLoaders(({ use: [, , postcss] }) => {
        const postcssOptions = postcss.options;
        postcss.options = { postcssOptions };
    }),
    // useEslintRc(),
    useEslintConfig(eslintConfig),
    useBabelRc(),
    (config) => {
        const loaders = config.module.rules.find(rule => Array.isArray(rule.oneOf)).oneOf;
        loaders[9].use.push({
            loader: 'style-resources-loader',
            options: {
                patterns: path.resolve(__dirname, 'src/assets/css/common.less')//全局引入公共的scss 文件
            }
        })
        return config
    }
)
